package de.cau.apo.problems.unrelatedscheduling;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents a machine for the unrelated scheduling problem.
 * It just consists of a fixed index and of list of jobs that are assigned to this machine.
 * 
 * @author Marius Richter
 *
 */
public class Machine {

	// ########### properties ###########
	
	/** the index of the machine */
	private final int index;
	/** the list of jobs that are assigned to this machine. */
	public final List<Job> jobs = new ArrayList<Job>();
	
	// ########### instantiation ###########

	/**
	 * Creates new machine object.
	 * @param index the index for this machine
	 */
	public Machine(int index) {
		this.index = index;
	}
	
	// ########### getter/setter ###########
	
	/**
	 * @return the index of this machine
	 */
	public int getIndex() {
		return index;
	}
	
	/**
	 * @return the list of jobs that are assigned to this machine
	 */
	public List<Job> getJobs() {
		return jobs;
	}
	
	/**
	 * @param job the job that shall be assigned to this machine
	 * @return true if adding was successful, false otherwise
	 */
	public boolean addJob(Job job) {
		return this.jobs.add(job);
	}
	
	/**
	 * @param job the job that shall be removed from this machine
	 * @return true if removing was successful, false otherwise
	 */
	public boolean removeJob(Job job) {
		return this.jobs.remove(job);
	}
	
	/**
	 * @return the makespan of this machine, that is the sum of the sizes 
	 * of all the jobs that are assigned to this machine
	 */
	public int getMakespan() {
		int makespan = 0;
		for (Job job : this.jobs) {
			makespan += job.getSize(this);
		}
		return makespan;
	}
	
	/**
	 * removes all the assigned jobs from this machine
	 */
	public void clear() {
		this.jobs.clear();
	}

	// ########### overriding toString() ###########

	@Override
	public String toString() {
		return "Machine [index=" + index + "]";
	}
	
}
